<?php
/**
 * 获取Word接口
 */
defined('IN_CMS') or exit('No permission resources.');

$userid = $_SESSION['userid'] ? $_SESSION['userid'] : (param::get_cookie('_userid') ? param::get_cookie('_userid') : param::get_cookie('userid'));
$siteid = param::get_cookie('siteid');
if(!$siteid) $siteid = get_siteid() ? get_siteid() : 1 ;

pc_base::load_sys_class('attachment','',0);
$module = trim($_GET['module']);
$catid = intval($_GET['catid']);
$attachment = new attachment($module,$catid,$siteid);
$attachment->set_userid($userid);
$a = $attachment->upload('file_upload','docx','','','',0,0);
if ($a) {
	$title = $attachment->uploadedfiles[0]['filename'];
	$filepath = $attachment->uploadedfiles[0]['filepath'];
	upload_json($a[0],pc_base::load_config('system','upload_url').$filepath,$title,format_file_size($attachment->uploadedfiles[0]['filesize']));
} else {
	dr_json(0, '文件上传失败');
}
if (!$filepath) {
	dr_json(0, '没有获取到文件内容');
}
if (!$title) {
	dr_json(0, '没有获取到文件标题');
}
$body = readWordToHtml(pc_base::load_config('system','upload_path').$filepath);
if (!$body) {
	dr_json(0, '没有获取到Word内容');
}

dr_json(1, '导入成功', array(
	'file' => pc_base::load_config('system','upload_url').$filepath,
	'title' => $title,
	'keyword' => get_keywords($title),
	'content' => $body,
));
function get_keywords($kw) {
	if (!$kw) {
		return '';
	}
	$cfg_bdqc_qcnum = pc_base::load_config('system', 'baidu_qcnum') ? pc_base::load_config('system', 'baidu_qcnum') : 10;
	if (pc_base::load_config('system', 'keywordapi')==1) {
		$baiduapi = pc_base::load_sys_class('baiduapi');
		$data = array(
			'title' => $kw,
			'content' => $kw,
		);
		$data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
		$baidu = $baiduapi->get_data('https://aip.baidubce.com/rpc/2.0/nlp/v1/keyword', $data, 1);
		if ($baidu && $baidu['data']['items']) {
			$n = 0;
			$resultstr = '';
			foreach ($baidu['data']['items'] as $t) {
				$resultstr .= ','.$t['tag'];
				$n++;
				if( $n >= $cfg_bdqc_qcnum ) break;
			}
		}
		return trim($resultstr, ',');
	} else if (pc_base::load_config('system', 'keywordapi')==2) {
		$XAppid = pc_base::load_config('system', 'xunfei_aid');
		$Apikey = pc_base::load_config('system', 'xunfei_skey');
		$fix = 0; //如果错误日志提示【time out|ilegal X-CurTime】，需要把$fix变量改为 100 、200、300、等等，按实际情况调试，只要是数字都行
		$XParam = base64_encode(json_encode(array(
			"type"=>"dependent",
		)));
		$XCurTime = SYS_TIME - $fix;
		$XCheckSum = md5($Apikey.$XCurTime.$XParam);
		$headers = array();
		$headers[] = 'X-CurTime:'.$XCurTime;
		$headers[] = 'X-Param:'.$XParam;
		$headers[] = 'X-Appid:'.$XAppid;
		$headers[] = 'X-CheckSum:'.$XCheckSum;
		$headers[] = 'Content-Type:application/x-www-form-urlencoded; charset=utf-8';
		$rt = json_decode(file_get_contents("http://ltpapi.xfyun.cn/v1/ke", false, stream_context_create(array(
			'http' => array(
				'method' => 'POST',
				'header' => $headers,
				'content' => http_build_query(array(
					'text' => $kw,
				)),
				'timeout' => 15*60
			)
		))), true);
		if (!$rt) {
			//showmessage('讯飞接口访问失败');
			return '';
		} elseif ($rt['code']) {
			//showmessage('讯飞接口: '.$rt['desc']);
			return '';
		} else {
			$n = 0;
			$resultstr = '';
			foreach ($rt['data']['ke'] as $t) {
				$resultstr .= ','.$t['word'];
				$n++;
				if( $n >= $cfg_bdqc_qcnum ) break;
			}
			return trim($resultstr, ',');
		}
	} else {
		$phpanalysis = pc_base::load_sys_class('phpanalysis');
		$phpanalysis = new phpanalysis('utf-8', 'utf-8', false);
		$phpanalysis->LoadDict();
		$phpanalysis->SetSource($kw);
		$phpanalysis->StartAnalysis(true);
		return $phpanalysis->GetFinallyKeywords($cfg_bdqc_qcnum);
	}
	return '';
}
/**
 * 附件添加如数据库
 * @param $uploadedfile 附件信息
 */
function add($uploadedfile) {
	$userid = $_SESSION['userid'] ? $_SESSION['userid'] : (param::get_cookie('_userid') ? param::get_cookie('_userid') : param::get_cookie('userid'));
	$siteid = param::get_cookie('siteid');
	if(!$siteid) $siteid = get_siteid() ? get_siteid() : 1 ;
	$att_db = pc_base::load_model('attachment_model');
	$uploadedfile['module'] = trim($_GET['module']);
	$uploadedfile['catid'] = intval($_GET['catid']);
	$uploadedfile['siteid'] = $siteid;
	$uploadedfile['userid'] = $userid;
	$uploadedfile['uploadtime'] = SYS_TIME;
	$uploadedfile['uploadip'] = ip();
	$uploadedfile['status'] = pc_base::load_config('system','attachment_stat') ? 0 : 1;
	$uploadedfile['authcode'] = md5($uploadedfile['filepath']);
	$uploadedfile['filename'] = file_name($uploadedfile['filename']);
	$uploadedfile['isimage'] = in_array($uploadedfile['fileext'], array('gif', 'jpg', 'jpeg', 'png', 'bmp')) ? 1 : 0;
	$aid = $att_db->api_add($uploadedfile);
	$uploadedfiles[] = $uploadedfile;
	return $aid;
}
/**
 * 设置upload上传的json格式cookie
 */
function upload_json($aid,$src,$filename,$size) {
	$arr['aid'] = intval($aid);
	$arr['src'] = trim($src);
	$arr['filename'] = urlencode($filename);
	$arr['size'] = $size;
	$json_str = json_encode($arr);
	$att_arr_exist = getcache('att_json', 'commons');
	$att_arr_exist_tmp = explode('||', $att_arr_exist);
	if(is_array($att_arr_exist_tmp) && in_array($json_str, $att_arr_exist_tmp)) {
		return true;
	} else {
		$json_str = $att_arr_exist ? $att_arr_exist.'||'.$json_str : $json_str;
		setcache('att_json', $json_str, 'commons');
		return true;
	}
}
function readWordToHtml($source) {
	include_once "vendor/autoload.php";
	$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
	$html = '';
	foreach ($phpWord->getSections() as $section) {
		foreach ($section->getElements() as $ele1) {
			$paragraphStyle = $ele1->getParagraphStyle();
			if ($paragraphStyle) {
				$html .= '<p style="text-align:'. $paragraphStyle->getAlignment() .';text-indent:20px;">';
			} else {
				$html .= '<p>';
			}
			if ($ele1 instanceof \PhpOffice\PhpWord\Element\TextRun) {
				foreach ($ele1->getElements() as $ele2) {
					if ($ele2 instanceof \PhpOffice\PhpWord\Element\Text) {
						$style = $ele2->getFontStyle();
						$fontFamily = mb_convert_encoding($style->getName(), 'GBK', 'UTF-8');
						$fontSize = $style->getSize();
						$isBold = $style->isBold();
						$styleString = '';
						$fontFamily && $styleString .= "font-family:{$fontFamily};";
						$fontSize && $styleString .= "font-size:{$fontSize}px;";
						$isBold && $styleString .= "font-weight:bold;";
						$html .= sprintf('<span style="%s">%s</span>',
							$styleString,
							mb_convert_encoding($ele2->getText(), 'GBK', 'UTF-8')
						);
					} elseif ($ele2 instanceof \PhpOffice\PhpWord\Element\Image) {
						$siteid = param::get_cookie('siteid');
						if(!$siteid) $siteid = get_siteid() ? get_siteid() : 1 ;
						if (pc_base::load_config('system','attachment_file')) {
							$dir = $siteid.'/'.date('Y/md/');
						} else {
							$dir = date('Y/md/');
						}
						$uploadpath = pc_base::load_config('system','upload_url').$dir;
						$uploaddir = pc_base::load_config('system','upload_path').$dir;
						$filename = substr(md5(SYS_TIME.uniqid().$ele2->getSource()), rand(0, 20), 15).'.'.$ele2->getImageExtension();
						$imageData = $ele2->getImageStringData(true);
						//$imageData = 'data:' . $ele2->getImageType() . ';base64,' . $imageData;
						$newfile = $uploaddir.$filename;
						file_put_contents($newfile, base64_decode($imageData));
						// 图片处理
						if (is_image($newfile)) {
							// 图片压缩处理
							if (intval($_GET['image_reduce'])) {
								$image->reduce($savefile, $_GET['image_reduce']);
							}
							pc_base::load_sys_class('image');
							$image = new image();
							$siteinfo = getcache('sitelist', 'commons');
							$config = string2array($siteinfo[$siteid]['setting']);
							$watermark_enable = $config['watermark_enable'];
							if(intval($_GET['watermark']) && intval($config['watermark_enable'])) {
								$config['source_image'] = $newfile;
								$config['dynamic_output'] = false;
								$image->watermark($config);
							}
						}
						$filepath = $dir.$filename;
						$downloadedfile = array('filename'=>$filename, 'filepath'=>$filepath, 'filesize'=>filesize($newfile), 'fileext'=>$ele2->getImageExtension());
						$aid = add($downloadedfile);
						upload_json($aid,pc_base::load_config('system','upload_url').$downloadedfile['filepath'],file_name($downloadedfile['filename']),format_file_size($downloadedfile['filesize']));
						$downloadedfiles[$aid] = $filepath;
						$html .= '<img src="'.$uploadpath.$filename.'" title="'.$filename.'" alt="'.$filename.'"/>';
					}
				}
			}
			$html .= '</p>';
		}
	}
	return mb_convert_encoding($html, 'UTF-8', 'GBK');
}
?>